package xyz.xpman.prisonerfitness.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.List;


@SuppressWarnings("unused")
public abstract class MyBaseAdapter<T, M extends IViewCache<T>> extends BaseAdapter {
    protected final int templateId;
    protected final List<T> items;
    protected final LayoutInflater layoutInflater;


    public MyBaseAdapter(@NotNull Activity context, int templateId, @NotNull List<T> items){
        this.items = items;
        this.templateId = templateId;
        this.layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        M viewCache;

        if (null == convertView) {
            convertView = layoutInflater.inflate(templateId, parent, false);

            viewCache = processViewCache(convertView);

            convertView.setTag(viewCache);
        } else {
            viewCache = (M) convertView.getTag();
        }

        viewCache.process(position, (T) getItem(position));

        return convertView;
    }


    protected abstract M processViewCache(View viewCache);

}
